<template>
  <div class="home">
    <div class="hero">
      <h1>
        <i class="fas fa-blog"></i> 烈焰之雨的Blog
      </h1>
      <p class="subtitle">分享技术，记录生活</p>
    </div>
    
    <div class="section-container articles-container">
      <div class="articles-section">
        <h2>最新文章</h2>
        
        <!-- 文章列表组件 -->
        <ArticleList
          :articles="articles"
          :loading="loading"
          :users="users"
          :categories="categories"
          empty-message="暂无文章发布"
          @article-click="viewArticle"
          @category-click="goToCategory"
        />
        
        <!-- 分页组件 -->
        <Pagination 
          v-if="articles.length > 0"
          :current-page="currentPage"
          :total-pages="totalPages"
          :max-visible="5"
          :show-jumper="totalPages > 5"
          @page-change="handlePageChange"
        />
      </div>
    </div>
    

    
  </div>
</template>

<script>
import { userAPI } from '@api/user.js'
import { articleAPI } from '@api/article.js'
import { dictAPI } from '@api/dict.js'
import Game2048 from '@components/Game2048.vue'
import QRCodeGenerator from '@components/QRCodeGenerator.vue'
import Pagination from '@components/Pagination.vue'
import ArticleList from '@components/ArticleList.vue'

export default {
  name: 'Home',
  components: {
    Game2048,
    QRCodeGenerator,
    Pagination,
    ArticleList
  },
  data() {
    return {
      loading: false,
      articles: [],
      users: [],
      categories: [],
      // 分页相关
      currentPage: 1,
      pageSize: 5,  // 每页显示 5 篇文章
      totalPages: 0,
      totalArticles: 0
    }
  },
  async mounted() {
    await this.loadArticles()
    await this.loadUsers()
    await this.loadCategories()
  },
  methods: {
    async loadArticles() {
      this.loading = true
      try {
        const response = await articleAPI.getPublishedArticlesPageWithStats(this.currentPage, this.pageSize)
        if (response.data.success) {
          this.articles = response.data.data
          this.totalPages = response.data.pages
          this.totalArticles = response.data.total
        }
      } catch (error) {
        console.error('加载文章失败:', error)
      } finally {
        this.loading = false
      }
    },

    async loadUsers() {
      try {
        const response = await userAPI.getAllUsers()
        if (response.data.success) {
          this.users = response.data.data
        }
      } catch (error) {
        console.error('加载用户失败:', error)
      }
    },

    async loadCategories() {
      try {
        const response = await dictAPI.getArticleCategories()
        if (response.data.success) {
          this.categories = response.data.data
        }
      } catch (error) {
        console.error('加载分类失败:', error)
      }
    },



    viewArticle(article) {
      // 跳转到文章详情页
      this.$router.push(`/article/${article.id}`)
    },
    
    goToCategory(categoryValue) {
      // 跳转到分类页面
      this.$router.push(`/category/${categoryValue}`)
    },
    
    async handlePageChange(page) {
      this.currentPage = page
      await this.loadArticles()
      // 滚动到文章区域顶部
      const articlesSection = document.querySelector('.articles-section')
      if (articlesSection) {
        articlesSection.scrollIntoView({ behavior: 'smooth', block: 'start' })
      }
    },
    
    goToLogin() {
      this.$router.push('/login')
    },
    
    goToAdmin() {
      this.$router.push('/admin')
    }
  }
}
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
  min-height: calc(100vh - 64px);
}

.hero {
  text-align: center;
  margin-bottom: 2rem;
  padding: 3rem 0;
  background: 
    linear-gradient(rgba(26, 26, 46, 0.8), rgba(22, 33, 62, 0.8)), 
    url('/images/hero-bg.jpg') center/cover no-repeat;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: white;
  margin: -2rem -2rem 2rem -2rem;
  position: relative;
  box-shadow: 
    0 4px 20px rgba(0, 0, 0, 0.3),
    0 8px 32px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  border: 1px solid rgba(100, 181, 246, 0.2);
  border-radius: 0 0 20px 20px;
}

.hero::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(100, 181, 246, 0.1) 0%, rgba(151, 220, 153, 0.1) 100%);
  z-index: 1;
}

.hero::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(100, 181, 246, 0.1) 0%, transparent 70%);
  animation: float 6s ease-in-out infinite;
  z-index: 1;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}


.hero h1 {
  font-size: 3rem;
  background: var(--gradient-rainbow);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 0.5rem;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  position: relative;
  z-index: 2;
}


.section-container {
  width: 100%;
}




.subtitle {
  color: #e0e0e0;
  font-size: 1.3rem;
  font-weight: 300;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  position: relative;
  z-index: 2;
}

.articles-section {
  text-align: left;
  position: relative;
  z-index: 1;
}

.articles-section h2 {
  font-size: 2rem;
  color: var(--text-primary);
  margin-bottom: 1.5rem;
  text-align: center;
  position: relative;
}

.articles-section h2::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: var(--gradient-primary);
  border-radius: var(--radius-sm);
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 3rem;
  color: #666;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.no-articles {
  text-align: center;
  padding: 3rem;
  color: #666;
  font-size: 1.1rem;
}

.articles-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1.5rem;
}

.article-item {
  background: linear-gradient(135deg, #363a4f 0%, #424549 100%);
  border-radius: var(--radius-md);
  border: 1px solid #5a5f7a;
  border-left: 4px solid var(--color-primary);
  transition: all var(--transition-normal);
  cursor: pointer;
  overflow: hidden;
  position: relative;
}

.article-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--gradient-rainbow);
  opacity: 0;
  transition: opacity var(--transition-normal);
}

.article-item:hover::before {
  opacity: 1;
}

.article-item:hover {
  transform: translateX(8px);
  box-shadow: var(--shadow-glow-blue);
  border-color: var(--color-primary-light);
}

.article-main {
  padding: 1.25rem 1.5rem;
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
  gap: 1rem;
}

.article-title {
  font-size: 1.05rem;
  color: var(--text-secondary);
  margin: 0;
  flex: 1;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-weight: 600;
  transition: color var(--transition-fast);
}

.article-title i {
  color: var(--color-primary-lighter);
  font-size: 0.9rem;
  opacity: 0.7;
}

.article-item:hover .article-title {
  color: var(--color-primary-light);
}

.article-status {
  padding: 0.25rem 0.6rem;
  border-radius: var(--radius-md);
  font-size: 0.7rem;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.article-status.published {
  background: linear-gradient(135deg, var(--color-success) 0%, var(--color-secondary-light) 100%);
  color: #1a1a1a;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.article-preview {
  color: var(--text-tertiary);
  line-height: 1.5;
  margin-bottom: 0.65rem;
  font-size: 0.875rem;
  opacity: 0.9;
}

.article-meta {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  font-size: 0.8rem;
  color: var(--text-tertiary);
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  opacity: 0.85;
}

.meta-item i {
  font-size: 0.75rem;
  opacity: 0.7;
}

.read-more-btn {
  background: var(--gradient-primary);
  color: white;
  padding: 0.3rem 0.75rem;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-weight: 600;
  transition: all var(--transition-normal);
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: auto;
}

.read-more-btn i {
  font-size: 0.7rem;
  transition: transform var(--transition-normal);
}

.read-more-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 12px rgba(92, 225, 255, 0.4);
}

.read-more-btn:hover i {
  transform: translateX(3px);
}





@media (max-width: 768px) {
  .hero {
    padding: 2rem 0;
    margin: -2rem -1rem 2rem -1rem;
  }
  
  .hero h1 {
    font-size: 2rem;
  }
  
  .subtitle {
    font-size: 1.1rem;
  }
}
</style>
